﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TodayWhoModel
{
    public class Helper
    {
        private readonly Random _rng = new Random();
        private const string _chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,_=-";

        public string RandomString(int size)
        {
            char[] buffer = new char[size];

            for (int i = 0; i < size; i++)
            {
                buffer[i] = _chars[_rng.Next(_chars.Length)];
            }
            return new string(buffer);
        }

        public static string ConvertToHttpLinks(string link)
        {
            string resLink = "";
            if(string.IsNullOrEmpty(link))
            {
                resLink = link;
            }
            else
            {
                
                if (link.Contains("http://"))
                {
                    resLink = link;
                }
                else
                {
                    resLink = "http://" + link;
                }
            }
            return resLink;
        }

        public static bool IsKeywordinUserName(string username)
        {   
            bool res = false;
            if (string.Compare(username, "general", true) == 0)
            { 
                res=true;
            }
            else if (string.Compare(username, "music", true) == 0)
            {
                res = true;
            }
            else if (string.Compare(username, "sport", true) == 0)
            {
                res = true;
            }
            else if (string.Compare(username, "arts", true) == 0)
            {
                res = true;
            }
            else if (string.Compare(username, "literature", true) == 0)
            {
                res = true;
            }
            else if (string.Compare(username, "tech", true) == 0)
            {
                res = true;
            }
            else if (string.Compare(username, "adm", true) == 0)
            {
                res = true;
            }
            else if (string.Compare(username, "images", true) == 0)
            {
                res = true;
            }
            else if (string.Compare(username, "jcrop", true) == 0)
            {
                res = true;
            }
            else if (string.Compare(username, "scripts", true) == 0)
            {
                res = true;
            }
            else if (string.Compare(username, "style", true) == 0)
            {
                res = true;
            }

            return res;
        }

        // Create a hash of the given password and salt.
        public static string CreateHash(string password, string salt)
        {
            // Get a byte array containing the combined password + salt.
            string authDetails = password + salt;
            byte[] authBytes = System.Text.Encoding.ASCII.GetBytes(authDetails);

            // Use MD5 to compute the hash of the byte array, and return the hash as
            // a Base64-encoded string.
            var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashedBytes = md5.ComputeHash(authBytes);
            string hash = Convert.ToBase64String(hashedBytes);

            return hash;
        }

        // Check to see if the given password and salt hash to the same value
        // as the given hash.
        public static bool IsMatchingHash(string password, string salt, string hash)
        {
            // Recompute the hash from the given auth details, and compare it to
            // the hash provided by the cookie.
            return CreateHash(password, salt) == hash;
        }

        
    }
}
